//
//  main.m
//  省市区--解析为JSON格式
//
//  Created by mafeng on 16/7/11.
//  Copyright © 2016年 mafeng. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Province.h"
#import "City.h"
#import "Zone.h"
#import "TempCity.h"
int main(int argc, const char * argv[]) {
#if 0
    NSString *path = @"/Users/dllo/Downloads/Homework-05/area.txt";
    NSString *areaStr = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    //     将省市区进行分割处理, 省里包含市, 市里包含区
    //     1. 先用数组字典表示
    NSArray *allArray = [areaStr componentsSeparatedByString:@"\n"];
//    NSMutableArray *china = [NSMutableArray array];
//    for (NSString *name in allArray) {
//        if (![name hasPrefix:@" "]) {
//            /**创建省字典, 包含两个字段 */
//            NSMutableDictionary *provinceDic = [NSMutableDictionary dictionaryWithCapacity:20];
//            [provinceDic setObject:name forKey:@"provinceName"];
//            [china addObject:provinceDic];
//        }else if(![name hasPrefix:@"    "]){
//          
//            
//            // 向市数组中添加是字典
//            
//            // 取到所有的县
//        }else{
//            /** 通过生数组取到当前所在省的字典 */
//         
//            /** 通过省字典取到市数组 */
//            
//            /** 通过市数组取到对应得市字典 */
//            
//            /** 通过市数组取到对应的县数组 */
//            
//        }
//    }
//
    NSMutableArray *allProArray = [NSMutableArray array];
    for (NSString *area in allArray) {
        if (![area hasPrefix:@" "]) {
            Province *pro = [Province zoneWithName:area];
            [allProArray addObject:pro];
        } else if(![area hasPrefix:@"    "]) {
            Province *pro = [allProArray lastObject];
            City *city = [City zoneWithName:area];
            [pro.zoneArray addObject:city];
        } else {
            City *city = [[[allProArray lastObject] zoneArray] lastObject];
            Zone *zone = [Zone zoneWithName:area];
            [city.zoneArray addObject:zone];
        }
    }
   // NSLog(@"%@", allProArray);
#endif
    // 第五题
    NSString *location = @"/Users/dllo/Downloads/Homework-05/Location1.txt";
    NSString *source = [NSString stringWithContentsOfFile:location encoding:NSUTF8StringEncoding error:nil];

    // 根据,分割字符串, 得到所有城市的数组
    NSArray *allCityArray = [source componentsSeparatedByString:@","];
#if 0
    NSMutableArray *allCityDicArray = [NSMutableArray array];
    // 根据每个城市中的|处理字符串, 重新生成字典的数组
    for (NSString *city in allCityArray) {
        // 根据|切割字符串
        NSArray *arrayCity = [city componentsSeparatedByString:@"|"];
        // 根据生成的数组, 生成新的城市字典
        NSMutableDictionary *cityDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[arrayCity firstObject], @"cityName", [arrayCity lastObject], @"cityNumber", nil];
        // 添加到新的数组当中
        [allCityDicArray addObject:cityDic];
    }
    // 排序(重要)
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
    [allCityDicArray sortUsingDescriptors:@[sort]];
    NSLog(@"%@", allCityDicArray);
    
    NSMutableArray *finalArray = [NSMutableArray array];
    for (NSMutableDictionary *cityDic in allCityDicArray) {
        NSString *cityNumebr = cityDic[@"cityNumber"];
        // if else 只执行其中的一个
        if ([cityNumebr hasSuffix:@"0000"]) {
            NSMutableDictionary *proDic = [NSMutableDictionary dictionaryWithDictionary:cityDic];
            [proDic setObject:[NSMutableArray array] forKey:@"cityArray"];
            [finalArray addObject:proDic];
        } else if ([cityNumebr hasSuffix:@"00"]) {
            NSMutableDictionary *proDic = [finalArray lastObject];
            NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
            NSMutableDictionary *dicCity = [NSMutableDictionary dictionaryWithDictionary:cityDic];
            [dicCity setObject:[NSMutableArray array] forKey:@"zoneArray"];
            [cityArray addObject:dicCity];
        } else {
           // NSMutableArray *zoneArray = [[[[finalArray lastObject] objectForKey:@"cityArray"] lastObject] objectForKey:@"zoneArray"];
            NSMutableDictionary *proDic = [finalArray lastObject];
            NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
            NSMutableDictionary *dicCity = [cityArray lastObject];
            NSMutableArray *zoneArray = [dicCity objectForKey:@"zoneArray"];
            NSMutableDictionary *zoneDic = [NSMutableDictionary dictionaryWithDictionary:cityDic];
            [zoneArray addObject:zoneDic];
        }
    }
    NSLog(@"%@", finalArray);
#endif
    /**********************model***********************/
    NSMutableArray *allTempCityArray = [NSMutableArray array];
    for (NSString *cityString in allCityArray) {
        // 根据|切割字符串
        NSArray *city = [cityString componentsSeparatedByString:@"|"];
        [city firstObject];// firstObject 对越界做了处理, 当数组为空时,返回空.
        TempCity *tempcity = [[TempCity alloc] initWithName:[city firstObject] number:[city lastObject]];
        [allTempCityArray addObject:tempcity];
    }
    // 对 类数组 进行排序
    NSSortDescriptor *sortTempCity = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
    [allTempCityArray sortUsingDescriptors:@[sortTempCity]];
    //NSLog(@"%@", allTempCityArray);
    NSMutableArray *finalClassArray = [NSMutableArray array];
    for (TempCity *tempCity in allTempCityArray) {
        if ([tempCity.cityNumber hasSuffix:@"0000"]) {
            Province *pro = [Province zoneWithTempCity:tempCity];
            [finalClassArray addObject:pro];
        } else if ([tempCity.cityNumber hasSuffix:@"00"]) {
            City *city = [City zoneWithTempCity:tempCity];
            Province *pro = [finalClassArray lastObject];
            [pro.zoneArray addObject:city];
        } else {
            Zone *zone = [Zone zoneWithTempCity:tempCity];
            Province *pro = [finalClassArray lastObject];
            City *city = [pro.zoneArray lastObject];
            [city.zoneArray addObject:zone];
        }
    }
    NSLog(@"%@", finalClassArray);
    return 0;
}
